<template>
  <el-popover placement="bottom-end"
              width="400"
              trigger="click">
    <el-form label-position="top"
             label-width="80px"
             :model="formData">
      <el-form-item v-for="(item, key) in formData"
                    :key="key"
                    :label="item.name"
                    :prop="key"
                    style="margin: 0">
        <!-- input -->
        <el-input v-if="item.type === 'input'"
                  :placeholder="item.placeholder"
                  v-model="item.value"
                  clearable
                  :maxlength="item.maxlength"></el-input>
        <!-- date -->
        <el-date-picker v-if="item.type === 'date'"
                        style="width: 100%"
                        v-model="item.value"
                        :type="item.dateType"
                        :placeholder="item.placeholder"
                        :value-format="item.valueFormat">
        </el-date-picker>
        <!-- select -->
        <el-select v-if="item.type === 'select'"
                   style="width: 100%"
                   v-model="item.value"
                   :placeholder="item.placeholder"
                   clearable>
          <el-option v-for="select in item.data"
                     :key="select.value"
                     :label="select.label"
                     :value="select.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="display: flex; justify-content: flex-end; margin-top: 12px">
      <el-button style="margin-right: 10px"
                 type=""
                 @click="reset()">重置</el-button>
      <el-button type="primary"
                 @click="search()">搜索</el-button>
    </div>
    <el-button slot="reference"
               icon="el-icon-d-arrow-right">筛选过滤</el-button>
  </el-popover>
</template>
<script>
export default {
  props: {
    config: {
      type: Object,
      default: () => { },
    },
  },
  data () {
    return {
      formData: this.config,
    };
  },
  methods: {
    reset () {
      for (const key in this.formData) {
        const item = this.formData[key];
        this.$set(item, "value", "");
      }
    },
    search () {
      const result = {};
      for (const key in this.formData) {
        const item = this.formData[key];
        result[key] = item.value;
      }
      this.$emit("change", result);
    },
  },
};
</script>
